package com.hymjweb.demo.hymj.basic_data_manage.xt_dic.service;

import com.hymjweb.demo.framework.cache.service.GlobalCacheService;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_dic.bean.XtDicCachePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_dic.bean.XtDicPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_dic.mapper.XtDicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class XtDicService extends BaseService {
    @Autowired
    private XtDicMapper xtDicMapper;

    @Autowired
    private GlobalCacheService globalCacheService;

    public List<XtDicCachePojo> findCacheAll() {
        List<XtDicCachePojo> pojos = xtDicMapper.findCacheAll();
        return pojos;
    }

    public Object getPageList(Map map) {
        this.pageListInit(map);
        List list = xtDicMapper.findAll(map);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }

    public List<XtDicPojo> queryChildNodeById(String id) {
        return xtDicMapper.queryChildNodeById(id);
    }

    public XtDicPojo bulidPojo(Map param) {
        XtDicPojo obj = new XtDicPojo();
        Integer id = DataTypeUtil.toInt(param.get("id"));
        Integer pid = DataTypeUtil.toInt(param.get("pid"));
        String name = DataTypeUtil.toNotNullString(param.get("name"));
        String fullName = DataTypeUtil.toNotNullString(param.get("fullName"));
        String dicKey = DataTypeUtil.toNotNullString(param.get("dicKey"));
        Integer status = DataTypeUtil.toInt(param.get("status"));
        String standby1 = DataTypeUtil.toNotNullString(param.get("standby1"));
        String standby1Desc = DataTypeUtil.toNotNullString(param.get("standby1Desc"));
        String standby2 = DataTypeUtil.toNotNullString(param.get("standby2"));
        String standby2Desc = DataTypeUtil.toNotNullString(param.get("standby2Desc"));
        String standby3 = DataTypeUtil.toNotNullString(param.get("standby3"));
        String standby3Desc = DataTypeUtil.toNotNullString(param.get("standby3Desc"));
        obj.setId(id);
        obj.setPid(pid);
        obj.setName(name);
        obj.setFullName(fullName);
        obj.setDicKey(dicKey);
        obj.setStatus(status);
        obj.setStandby1(standby1);
        obj.setStandby1Desc(standby1Desc);
        obj.setStandby2(standby2);
        obj.setStandby2Desc(standby2Desc);
        obj.setStandby3(standby3);
        obj.setStandby3Desc(standby3Desc);
        obj.setCreateTime(this.getSysTime());
        obj.setCreateUserId(this.getLoginCzyId());
        obj.setUpdateTime(this.getSysTime());
        obj.setUpdateUserId(this.getLoginCzyId());
        return obj;
    }

    // @OperationLogger(oper = SysConstant.OPER_ADD, operTitle="数据字典-新增" , operMemo = "", classType = XtDicPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(XtDicPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("字典新增参数错误,新增失败!");
        }

        //判断id是否重复
        XtDicPojo dbXtDicPojo = xtDicMapper.findObjectById(pojo.getId());
        if (dbXtDicPojo != null) {
            throwException("数据字典新增ID冲突,新增失败!");
        }
        //判断key值是否重复
        if (!DataTypeUtil.isEmptyStr(pojo.getDicKey())) {
            List<XtDicPojo> dicPojoList = xtDicMapper.queryXtDicPojoByDicKey(pojo.getDicKey());
            if (dicPojoList.size() > 0) {
                throwException("数据字典新增Key有重复,新增失败!");
            }
        }
        //判断pid是否输入有误
        XtDicPojo dbPidXtDicPojo = xtDicMapper.findObjectById(pojo.getPid());
        if (dbPidXtDicPojo == null) {
            throwException("数据字典新增PID不存在,新增失败!");
        }

        int affectRows = xtDicMapper.insert(pojo);
        if (affectRows > SysConstant.INT_ZERO) {
            // 重新加载字典表缓存
            globalCacheService.loadXtDicCache();
        }
        return affectRows;
    }

    // @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle="数据字典-修改" , operMemo = "", classType = XtDicPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int update(XtDicPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("数据字典更新参数错误,更新失败!");
        }

        String xtDickKey = pojo.getDicKey();

        if (xtDickKey != null && !DataTypeUtil.isEmptyStr(xtDickKey)) {
            List<XtDicPojo> dicPojoList = xtDicMapper.queryXtDicPojoByDicKey(xtDickKey);
            if (dicPojoList.size() > 1) {
                throwException("数据字典更新Key有重复,更新失败!");
            }
        }

        int affectRows = xtDicMapper.update(pojo);
        if (affectRows > SysConstant.INT_ZERO) {
            // 重新加载字典表缓存
            globalCacheService.loadXtDicCache();
        }

        return affectRows;
    }

    // @OperationLogger(oper = SysConstant.OPER_DELETE, operTitle="数据字典-删除" , operMemo = "", classType = XtDicPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int delete(String id) throws Exception {
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("数据字典删除参数错误,删除失败!");
        }
        //如果有子节点，不允许删除
        List<XtDicPojo> childNodeList = this.queryChildNodeById(id);
        if (childNodeList != null && !childNodeList.isEmpty()) {
            throwException("数据字典删除对象下存在子节点,删除失败!");
        }
        int affectRows = xtDicMapper.delete(id);
        if (affectRows > SysConstant.INT_ZERO) {
            // 重新加载字典表缓存
            globalCacheService.loadXtDicCache();
        }
        return affectRows;
    }


}
